﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;

namespace LGSVR.Common
{
    public abstract class BasePage : System.Web.UI.Page
    {
        protected override void OnInitComplete(EventArgs e)
        {
            MsgContent = GetSessionPromptMessage();
            base.OnInitComplete(e);
        }
        /// <summary>
        /// 操作结果反馈
        /// </summary>
        public virtual  string MsgContent { get; set; }
        /// <summary>
        /// 分页HTML代码
        /// </summary>
        public virtual string SplitHtmlCode { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        public static int PageSize = 20;
        /// <summary>
        /// Session操作提示信息
        /// </summary>
        public const string Session_PromptMessage = "LG_PromptMessage";

        /// <summary>
        /// 提示类型
        /// </summary>
        public enum MessageType
        {
            /// <summary>
            /// 错误
            /// </summary>
            Error = 1,
            /// <summary>
            /// 警告
            /// </summary>
            Warn,
            /// <summary>
            /// 成功
            /// </summary>
            Success,
            /// <summary>
            /// 提示
            /// </summary>
            Prompt
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="msg">要显示的提示内容</param>
        /// <param name="msgType">消息类型</param>
        /// <param name="isHideMainBody">是否显示主体内容</param>
        /// <param name="isClose">是否自动关闭提示</param>
        /// <returns></returns>
        public virtual string ShowMessage(string msg, MessageType msgType, bool isHideMainBody, bool isClose)
        {
            StringBuilder sbMsgContent;

            sbMsgContent = new StringBuilder();

            switch (msgType)
            {
                case MessageType.Error:
                    sbMsgContent.Append(" <div class=\"notification error png_bg\">");
                    sbMsgContent.Append("<a href=\"#\" class=\"close\"><img src=\"/skins/default/images/cross_grey_small.png\" title=\"关闭\" alt=\"关闭\" /></a>");
                    sbMsgContent.Append("<div>");
                    sbMsgContent.Append(msg);
                    sbMsgContent.Append(" </div>");
                    sbMsgContent.Append("<span id=\"PromptInfo_Timer\" class=\"timer\"></span>");
                    sbMsgContent.Append("</div>");
                    sbMsgContent.Append("<script type=\"text/javascript\">displayPromptInfo(" + isHideMainBody.ToString().ToLower() + "," + isClose.ToString().ToLower() + ");</script>");
                    break;
                case MessageType.Success:
                    sbMsgContent.Append(" <div class=\"notification success png_bg\">");
                    sbMsgContent.Append("<a href=\"#\" class=\"close\"><img src=\"/skins/default/images/cross_grey_small.png\" title=\"关闭\" alt=\"关闭\" /></a>");
                    sbMsgContent.Append("<div>");
                    sbMsgContent.Append(msg);
                    sbMsgContent.Append(" </div>");
                    sbMsgContent.Append("<span id=\"PromptInfo_Timer\" class=\"timer\"></span>");
                    sbMsgContent.Append("</div>");
                    sbMsgContent.Append("<script type=\"text/javascript\">displayPromptInfo(" + isHideMainBody.ToString().ToLower() + "," + isClose.ToString().ToLower() + ");</script>");
                    break;
                case MessageType.Warn:
                    sbMsgContent.Append(" <div class=\"notification attention png_bg\">");
                    sbMsgContent.Append("<a href=\"#\" class=\"close\"><img src=\"/skins/default/images/cross_grey_small.png\" title=\"关闭\" alt=\"关闭\" /></a>");
                    sbMsgContent.Append("<div>");
                    sbMsgContent.Append(msg);
                    sbMsgContent.Append(" </div>");
                    sbMsgContent.Append("<span id=\"PromptInfo_Timer\" class=\"timer\"></span>");
                    sbMsgContent.Append("</div>");
                    sbMsgContent.Append("<script type=\"text/javascript\">displayPromptInfo(" + isHideMainBody.ToString().ToLower() + "," + isClose.ToString().ToLower() + ");</script>");
                    break;
                case MessageType.Prompt:
                    sbMsgContent.Append(" <div class=\"notification information png_bg\">");
                    sbMsgContent.Append("<a href=\"#\" class=\"close\"><img src=\"/skins/default/images/cross_grey_small.png\" title=\"关闭\" alt=\"关闭\" /></a>");
                    sbMsgContent.Append("<div>");
                    sbMsgContent.Append(msg);
                    sbMsgContent.Append(" </div>");
                    sbMsgContent.Append("<span id=\"PromptInfo_Timer\" class=\"timer\"></span>");
                    sbMsgContent.Append("</div>");
                    sbMsgContent.Append("<script type=\"text/javascript\">displayPromptInfo(" + isHideMainBody.ToString().ToLower() + "," + isClose.ToString().ToLower() + ");</script>");
                    break;
            }

            return sbMsgContent.ToString();
        }

        /// <summary>
        /// 防刷新
        /// </summary>
        public virtual void NoRefresh()
        {
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            HttpContext.Current.Response.AddHeader("pragma", "no-cache");
            HttpContext.Current.Response.CacheControl = "no-cache";
            HttpContext.Current.Session[Session_PromptMessage] = MsgContent;
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery, false);
        }

        /// <summary>
        /// 操作提示信息
        /// </summary>
        /// <returns></returns>
        public virtual string GetSessionPromptMessage()
        {
            string msgContent;

            if (HttpContext.Current.Session[Session_PromptMessage] != null)
            {
                msgContent = HttpContext.Current.Session[Session_PromptMessage].ToString();
                HttpContext.Current.Session.Remove(Session_PromptMessage);
            }
            else
            {
                msgContent = string.Empty;
            }

            return msgContent;
        }
    }
}
